package com.example.observepattern.client;

import com.example.observepattern.observer.impl.WeatherStation;
import com.example.observepattern.subject.impl.EmailAlertService;
import com.example.observepattern.subject.impl.PhoneApp;
import com.example.observepattern.subject.impl.Website;

//模拟场景
public class Client {
    public static void main(String[] args) {
        // 1. 创建气象站（被观察者）
        WeatherStation weatherStation= new WeatherStation();

        // 2. 创建订阅者（观察者）
        PhoneApp phoneApp = new PhoneApp();
        Website website = new Website();
        EmailAlertService emailService = new EmailAlertService();

        // 3. 订阅者去气象站注册
        weatherStation.registryObserver(phoneApp);
        weatherStation.registryObserver(website);
        weatherStation.registryObserver(emailService);

        // 4.气象站更新数据，观察者自动收到通知
        weatherStation.setMeasurements(25.0f, 60.0f, 1013.0f);
        // 5.气象站更新数据，观察者自动收到通知
        weatherStation.notifyObservers();


        //  网站退订
        weatherStation.removeObserver(website);


        // 再次更新，只有手机和邮件收到通知
        weatherStation.setMeasurements(32.0f, 50.0f, 1008.0f);

    }
}
